package org.azmat.springcache.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.azmat.springcache.SQL.Entity.Book;
import org.azmat.springcache.SQL.Service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class BookController {

    @Autowired
    private BookService bookService;

    // 查询所有书籍
    @GetMapping("/books")
    public ModelAndView getBooksByPage(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "10") int size,
            @RequestParam(value = "search", required = false) String search) {

        ModelAndView modelAndView = new ModelAndView();
        IPage<Book> pageRequest = new Page<>(page, size);
        IPage<Book> resultPage = bookService.page(pageRequest, search);

        modelAndView.addObject("books", resultPage.getRecords());
        modelAndView.addObject("currentPage", resultPage.getCurrent());
        modelAndView.addObject("totalPages", resultPage.getPages());
        modelAndView.addObject("totalRecords", resultPage.getTotal());
        modelAndView.addObject("search", search);
        modelAndView.setViewName("booksList");

        return modelAndView;
    }

    // 根据id查询书籍
    @GetMapping("/book/{id}")
    public ModelAndView getById(@PathVariable Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        Book book = bookService.getById(id);
        modelAndView.addObject("book", book);
        modelAndView.setViewName("bookDetail");
        return modelAndView;
    }

    // 新增书籍
    @GetMapping("/book/new")
    public ModelAndView newBookForm() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("bookForm");
        modelAndView.addObject("book", new Book());
        return modelAndView;
    }

    @PostMapping("/book")
    public String saveBook(@ModelAttribute Book book) {
        if (bookService.BookSave(book)) {
            return "redirect:/books";
        }
        return "redirect:/book/new";
    }

    // 删除书籍
    @PostMapping("/book/delete/{id}")
    public String deleteBook(@PathVariable Integer id) {
        bookService.removeById(id);
        return "redirect:/books";
    }

    // 修改书籍
    @GetMapping("/book/edit/{id}")
    public ModelAndView editBookForm(@PathVariable Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        Book book = bookService.getById(id);
        modelAndView.addObject("book", book);
        modelAndView.setViewName("bookForm");
        return modelAndView;
    }

    @PostMapping("/book/update")
    public String updateBook(@ModelAttribute Book book) {
        bookService.updateById(book);
        return "redirect:/books";
    }
}
